<template>
    <div class="container-fluid" v-loading="loading">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header"><h4>文章创建</h4></div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8">
                                <div class="form-group">
                                    <label for="title">标题</label>
                                    <input type="text" class="form-control" v-model="title" placeholder="标题..">
                                </div>
                                <div class="form-group">
                                    <label for="summary">摘要</label>
                                    <el-input
                                            type="textarea"
                                            placeholder="请输入摘要"
                                            v-model="describe"
                                            maxlength="100"
                                            :rows="4"
                                            show-word-limit>
                                    </el-input>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="imageUrl">视频</label>
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/api/upload?module=media"
                                            :show-file-list="false"
                                            :on-success="handleVideoUrlSuccess"
                                            accept="video/*"
                                            :before-upload="beforeAvatarUpload">

                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </div>
                            </div>

                        </div>


                        <div class="form-group">
                            <label for="imageUrl">视频</label>
                            <video id="myVideo" class="video-js" ref="video" controls="controls">
                                <source :src="videoUrl" type="video/mp4">
                            </video>
                        </div>

                        <div class="form-group">
                            <p>
                                <button type="submit" class="btn btn-primary" v-on:click="submit()">提交</button>
                            </p>
                            <p>
                                <button class="btn btn-primary" v-on:click="changeImg()">修改封面图</button>
                                <button class="btn btn-primary" v-on:click="changeImg(true)">指定当前帧生成封面图</button>
                            </p>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "VideoCreateComponent",
        data() {
            return {
                title: '',
                describe:'',
                imageUrl:'',
                videoUrl:'',
                loading:false,
                player:null
            }
        },
        computed: {

        },
        methods:{
            handleVideoUrlSuccess: function (response) {
                let self = this;
                if (response.code === 200) {
                    self.videoUrl = response.url;
                    self.changeImg();
                    self.$nextTick(() => {
                        self.playerInit();
                    });

                }
                this.loading = false;
                //console.log(response)
            },
            beforeAvatarUpload: function (item) {
                this.loading = true;
            },
            changeImg(currentTime=false){
                let self = this;

                if (currentTime == false) {
                    var currentTimeValue = (Math.ceil(Math.random()*20)) * 1000;
                } else {
                    var currentTimeValue = (this.player.currentTime()) * 1000;
                }


                self.imageUrl = self.videoUrl+'?x-oss-process=video/snapshot,t_'+parseInt(currentTimeValue)+',f_jpg';
            },
            playerInit:function() {
                this.player =  this.$video(myVideo, {
                    //确定播放器是否具有用户可以与之交互的控件。没有控件，启动视频播放的唯一方法是使用autoplay属性或通过Player API。
                    // controls: true,
                    //自动播放属性,muted:静音播放
                    //autoplay: "muted",
                    //建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
                    //preload: "auto",
                    //设置视频播放器的显示宽度（以像素为单位）
                    width: "250px",
                    //设置视频播放器的显示高度（以像素为单位）
                    height: "250px",

                    fullScreen: {
                        controlBar: true
                    }
                });
            },
            submit:function () {
                let self = this;
                let uri = "video/store";

                if (!self.title) {
                    self.$message.error("必须填写标题");
                    return;
                }
                /*if (!self.describe) {
                    self.$message.error("必须填写摘要");
                    return;
                }*/
                if (!self.imageUrl) {
                    self.$message.error("必须上传封面图");
                    return;
                }
                if(!self.videoUrl){
                    self.$message.error("必须上传视频");
                    return;
                }
                self.loading = true;
                let postData = {
                    title: self.title, describe: self.describe,
                    imageUrl:self.imageUrl,videoUrl:self.videoUrl};

                axios.post(uri, postData).then(function (result) {
                    if (result) {
                        window.location.href = '/admin/video/index';
                    } else {
                        self.$message.error("创建视频失败！");
                    }
                }).catch(function (error) {
                    self.loading = false;
                });
            },
        },
        mounted() {
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .el-upload__input{
        display: none;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
    .avatar {
        width: 120px;
        height: 120px;
        display: block;
    }
</style>
